if test -n "$__UNZIP_LOADED"; then
    return
fi
__UNZIP_LOADED=1

# 解压文件，自动创建子目录
# Example usage:
# unzip_and_organize <archive_path> <target_dir> <unzip command and options>
unzip_and_organize() {
    local zip_file="$1"
    local target_directory="$2"
    shift 2

    # Create a temporary directory
    local temp_dir=$(mktemp -d)
    local unique_link_name="temp_$(date +%s_%N)"

    # Create a symbolic link to the zip file in the temporary directory
	zip_realpath=`realpath "$zip_file"`
    ln -s "$zip_realpath" "$temp_dir/$unique_link_name"

    # Change to the temporary directory and execute the unzip command
    (
        cd "$temp_dir" || exit
        eval "$*" "$unique_link_name"
    )

    # Remove the symbolic link
    rm "$temp_dir/$unique_link_name"

    # Check the contents of the temporary directory
    items_in_temp=$(ls "$temp_dir")

    # Count the number of items in the temporary directory
    item_count=$(echo "$items_in_temp" | wc -l)

    if [ "$item_count" -eq 1 ] && [ -d "$temp_dir/$items_in_temp" ]; then
        # If there's only one directory, move it to the target directory
        single_dir="$temp_dir/$items_in_temp"
        mv "$single_dir" "$target_directory/"
    else
        # Otherwise, rename the temporary directory to the base name of the zip file and move it
        base_name=$(basename "$zip_file" .zip)
        mv "$temp_dir" "$target_directory/$base_name"
    fi

    # Clean up the last temporary directory if any files were left
    rmdir "$temp_dir" 2>/dev/null || true
}

